﻿Public Class frmPrdInOut
    Dim dsPrdInOut As DataSet
    Dim daPrdInOut As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim able1Comma, able2Comma, able3Comma, able4Comma As String
    Dim gridHeaderChecked As Boolean = False
    Dim autoCount As Boolean = True

    Private Sub frmPrdInOut_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub frmPrdInOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        Call dbConnection()
        Call gSubMkCmbCompany("", cmbCompany)
        Call gSubMkCmbCompany("", cmbFindCompany)
        Call mkCmbProduct("", cmbPrdCd)
        Call mkCmbInCause(cmbCause)
        gSubMkCmbFlag(cmbFlag) '## 삭제 여부 콤보박스 초기화
        cmbFlag.SelectedIndex = 0
        cmbFindType.SelectedIndex = 0
        Call gridRefresh("default")
    End Sub

    Private Sub cmbPrdCd_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbPrdCd.KeyPress
        e.Handled = True
    End Sub

    '## 실시간 제품 단가 가져오기
    Private Sub cmbPrdCd_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbPrdCd.SelectedIndexChanged
        If prdCdChangeActionOn = True Then
            If cmbPrdCd.SelectedValue.ToString <> "0" Then
                sql = "SELECT unitInPrice FROM tblProduct WHERE prdCd=" + cmbPrdCd.SelectedValue.ToString
                dsPrdInOut = New DataSet
                daPrdInOut = New OleDb.OleDbDataAdapter(sql, dbConn)
                daPrdInOut.Fill(dsPrdInOut, "prdUintInPrice")

                dbConn.Close()

                txtUnitInPrice.Text = dsPrdInOut.Tables("prdUintInPrice").Rows(0).Item(0)
                txtUnitInPrice.Text = Format(CInt(txtUnitInPrice.Text), "#,##0")
            Else
                txtUnitInPrice.Text = 0
            End If
            Call getEaOnBox()
            Call getAllPrice()
        End If
    End Sub

    Private Sub grdPrdInOut_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPrdInOut.CellClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        If e.ColumnIndex = 1 Then
            Exit Sub
        End If
        If IsDBNull(grdPrdInOut.Rows(e.RowIndex).Cells(0).Value) = True Then
            Exit Sub
        End If
        If grdPrdInOut.Rows(e.RowIndex).Cells(2).Value = "출고" Then
            gSessionKey = grdPrdInOut.Rows(e.RowIndex).Cells(16).Value
            Dim MDIFormSales As New frmSales
            MDIFormSales.Show()
            Exit Sub
        End If
        Call prdInOutDetail(grdPrdInOut.Rows(e.RowIndex).Cells(0).Value)
    End Sub

    '## 그리드내의 체크박스 컨트롤
    Private Sub grdPrdInOut_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPrdInOut.CellContentClick

        If e.ColumnIndex = 1 Then
            If IsDBNull(grdPrdInOut.Rows(grdPrdInOut.CurrentCell.RowIndex).Cells(1).Value) = False Then
                If grdPrdInOut.Rows(grdPrdInOut.CurrentCell.RowIndex).Cells(1).Value = True Then
                    grdPrdInOut.Rows(grdPrdInOut.CurrentCell.RowIndex).Cells(1).Value = False
                Else
                    grdPrdInOut.Rows(grdPrdInOut.CurrentCell.RowIndex).Cells(1).Value = True
                End If

            Else
                grdPrdInOut.Rows(grdPrdInOut.CurrentCell.RowIndex).Cells(1).Value = True
            End If
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Call savePrdOut()
    End Sub

    Private Sub txtPrdNm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPrdNm.KeyUp

        Call mkCmbProduct(txtPrdNm.Text, cmbPrdCd)
    End Sub

    Private Sub txtCompNm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCompNm.KeyUp
        Call gSubMkCmbCompany(txtCompNm.Text, cmbCompany)
    End Sub

    Private Sub txtUnitPrice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUnitInPrice.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
            able1Comma = ""
        Else
            able1Comma = "ok"
        End If
    End Sub

    Private Sub txtUnitPrice_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUnitInPrice.KeyUp
        If able1Comma = "ok" Then
            gSubWithComma(txtUnitInPrice)
        End If
    End Sub

    Private Sub txtTotalPrice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTotalPrice.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
            able2Comma = ""
        Else
            able2Comma = "ok"
        End If
    End Sub

    Private Sub txtTotalPrice_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTotalPrice.KeyUp
        If able2Comma = "ok" Then
            gSubWithComma(txtTotalPrice)
        End If
    End Sub


    Private Sub txtQty_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQty.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
            able3Comma = ""
        Else
            able3Comma = "ok"
        End If
    End Sub

    Private Sub txtQty_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtQty.KeyUp
        If able3Comma = "ok" Then
            gSubWithComma(txtQty)
        End If
    End Sub

    Private Sub txtQty_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.Leave
        txtTotalPrice.Text = CDbl(txtUnitInPrice.Text) * CDbl(txtQty.Text)
        gSubWithComma(txtTotalPrice)
        Call getEaOnBox()
        Call getAllPrice()
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Call txtReset()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Call gridRefresh("")
    End Sub

    Private Sub btnSearchReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchReset.Click
        cmbFindType.SelectedIndex = 0
        cmbFindCompany.SelectedValue = 0
        Call gridRefresh("")
    End Sub

    Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Call gridRefresh("")
        End If
    End Sub

    Private Sub cmbFindType_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbFindType.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbFindType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFindType.SelectedIndexChanged
        If cmbFindType.SelectedIndex = 1 Then
            Call mkCmbOutCause(cmbFindCause)
        Else
            Call mkCmbInCause(cmbFindCause)
        End If
    End Sub

    Private Sub txtTotalQty_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTotalQty.Leave
        Call getAllPrice()
    End Sub

    Private Sub txtUnitInPrice_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUnitInPrice.Leave
        Call getAllPrice()
    End Sub

    Private Sub radSalesUnit1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSalesUnit1.CheckedChanged
        Call getEaOnBox()
        Call getAllPrice()
    End Sub

    Private Sub radSalesUnit2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSalesUnit2.CheckedChanged
        Call getEaOnBox()
        Call getAllPrice()
    End Sub

    Private Sub cmbCause_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbCause.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbCompany_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbCompany.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbFindCause_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbFindCause.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbFindCompany_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbFindCompany.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbFlag_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbFlag.KeyPress
        e.Handled = True
    End Sub

#Region "프로시져"

    '## 입출고 리스트
    Private Sub gridRefresh(ByVal initFlag As String)
        Call dbConnection()
        Dim addSql1, addSql2 As String
        Dim valFindCompany As String = ""
        Dim totalInput As Double = 0
        Dim totalOutPut As Double = 0
        Dim existCnt As Double = 0
        Dim totalAmt As Double = 0
        addSql1 = ""
        addSql2 = ""

        If cmbFindCompany.SelectedValue.ToString <> 0 Then
            valFindCompany = cmbFindCompany.SelectedValue.ToString
        End If
        If valFindCompany <> "" Then
            addSql2 = " WHERE B2.compCd=" + valFindCompany
        End If
        If txtSearch.Text <> "" Then
            addSql1 = " AND B1.prdNm LIKE '%" + txtSearch.Text + "%' "
        End If
        If cmbFindType.SelectedIndex = 1 Then
            addSql1 = addSql1 + " AND A1.[input] = 0 "
        ElseIf cmbFindType.SelectedIndex = 2 Then
            addSql1 = addSql1 + " AND A1.[outPut] = 0 "
        End If
        If cmbFindCause.IsHandleCreated = True Then
            If cmbFindCause.SelectedValue.ToString <> "-출고사유-" And cmbFindCause.SelectedValue.ToString <> "-입고사유-" And cmbFindCause.SelectedValue.ToString <> "" Then
                addSql1 = addSql1 + " AND A1.cause='" + cmbFindCause.SelectedValue.ToString + "' "
            End If
        End If
        If initFlag = "default" Then
            addSql1 = " AND A1.flag='N'"
        End If
        dsPrdInOut = New DataSet

        sql = "SELECT A.seq, A.issueDt as 거래일, A.logType as 구분, A.cause as 방식, A.prdNm as 제품, A.[output] as 출고, A.[input] as 입고, A.company as 거래처, B.memNm as 회원명, A.unitPrice as 단가, A.totalPrice as 총금액, A.orgAmount as 판매가, A.tax as 부가세, A.salesType as 도소매, A.dcRate+'%' as 할인율, A.dcAmount as 할인금액, A.sessionKey " + _
           "   FROM (" + _
           "               SELECT A2.seq, A2.logType, A2.cause, A2.prdNm, B2.company, A2.memId, A2.[output], A2.[input], A2.unitPrice, A2.totalPrice, A2.amount, A2.orgAmount, A2.tax, A2.salesType, A2.issueDt, A2.dcRate, A2.dcAmount, A2.sessionkey " + _
           "               FROM (" + _
           "                           SELECT A1.seq, IIF(A1.[input]>0, '입고', '출고') as  logType, A1.cause, B1.prdNm, A1.compCd, A1.memId, A1.[output], A1.[input], A1.unitPrice, A1.totalPrice, A1.amount, A1.orgAmount, A1.tax, A1.salesType, A1.issueDt, A1.dcRate, A1.dcAmount, A1.sessionkey  " + _
           "                           FROM tblPrdInOut A1 LEFT JOIN tblProduct B1 ON A1.prdCd=B1.prdCd " + _
           "                           WHERE A1.flag='Y'" + addSql1 + _
           "            ) A2 LEFT JOIN tblCompany B2 ON A2.compCd=B2.compCd " + addSql2 + _
           "    ) A LEFT JOIN tblMember B ON A.memId=B.memId" + _
            "    ORDER BY A.seq DESC "
        Try
            daPrdInOut = New OleDb.OleDbDataAdapter(sql, dbConn)
            daPrdInOut.Fill(dsPrdInOut, "PrdInOut")

            'Dim chkBox As New DataColumn("선택", System.Type.GetType("System.Boolean"))
            'dsPrdInOut.Tables("PrdInOut").Columns.Add(chkBox)
            'chkBox.SetOrdinal(1)

            grdPrdInOut.DataSource = dsPrdInOut.Tables("PrdInOut")

            lbGrid1Cnt.Text = "총 " + dsPrdInOut.Tables("PrdInOut").Rows.Count.ToString + " 건"
            For c = 0 To dsPrdInOut.Tables("PrdInOut").Rows.Count - 1
                totalInput = totalInput + CDbl(dsPrdInOut.Tables("PrdInOut").Rows(c).Item(6))
                totalOutPut = totalOutPut + CDbl(dsPrdInOut.Tables("PrdInOut").Rows(c).Item(5))
                totalAmt = totalAmt + CDbl(dsPrdInOut.Tables("PrdInOut").Rows(c).Item(10))
            Next
            existCnt = totalInput - totalOutPut
            lbSummery.Text = "입고 : " + Format(CInt(totalInput), "#,##0") + "   /   출고 : " + Format(CInt(totalOutPut), "#,##0") + "   /   재고 : " + Format(CInt(existCnt), "#,##0") + "   /   총합계 : " + Format(CInt(totalAmt), "#,##0")

            grdPrdInOut.Columns(8).Frozen = True
            dbConn.Close()
            Call setGridDesign()

        Catch err As ObjectDisposedException
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub

    Private Sub setGridDesign()

        grdPrdInOut.Columns(0).Visible = False
        grdPrdInOut.Columns(1).Width = "70"
        grdPrdInOut.Columns(2).Width = "60"
        grdPrdInOut.Columns(3).Width = "60"
        grdPrdInOut.Columns(4).Width = "110"
        grdPrdInOut.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdPrdInOut.Columns(5).Width = "55"
        grdPrdInOut.Columns(5).DefaultCellStyle.ForeColor = Color.Red
        grdPrdInOut.Columns(6).Width = "55"
        grdPrdInOut.Columns(6).DefaultCellStyle.ForeColor = Color.Blue

        grdPrdInOut.Columns(7).Width = "100"
        grdPrdInOut.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdPrdInOut.Columns(8).Width = "80"
        grdPrdInOut.Columns(9).Width = "90"
        grdPrdInOut.Columns(9).DefaultCellStyle.Format = "n0"
        grdPrdInOut.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdPrdInOut.Columns(10).Width = "90"
        grdPrdInOut.Columns(10).DefaultCellStyle.Format = "n0"
        grdPrdInOut.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdPrdInOut.Columns(11).Width = "90"
        grdPrdInOut.Columns(11).DefaultCellStyle.Format = "n0"
        grdPrdInOut.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdPrdInOut.Columns(12).Width = "70"
        grdPrdInOut.Columns(12).DefaultCellStyle.Format = "n0"
        grdPrdInOut.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdPrdInOut.Columns(13).Width = "70"
        grdPrdInOut.Columns(14).Width = "70"
        grdPrdInOut.Columns(15).Width = "80"
        grdPrdInOut.Columns(15).DefaultCellStyle.Format = "n0"
        grdPrdInOut.Columns(15).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdPrdInOut.Columns(16).Visible = False '## sessionKey

        With grdPrdInOut.DefaultCellStyle
            .Alignment = DataGridViewContentAlignment.MiddleCenter

        End With

        For i = 0 To grdPrdInOut.Rows.Count - 1
            If grdPrdInOut.Rows(i).Cells(2).Value = "출고" Then
                grdPrdInOut.Rows(i).DefaultCellStyle.BackColor = Color.LavenderBlush
            Else
                grdPrdInOut.Rows(i).DefaultCellStyle.BackColor = Color.LightCyan
            End If
        Next
    End Sub

    '## 총금액 구하기
    Private Sub getAllPrice()
        If txtTotalQty.Text = "" Then
            txtTotalQty.Text = 0
        End If
        If txtUnitInPrice.Text = "" Then
            txtUnitInPrice.Text = 0
        End If
        txtTotalPrice.Text = CDbl(txtTotalQty.Text) * (txtUnitInPrice.Text)
        txtTotalPrice.Text = Format(CInt(txtTotalPrice.Text), "#,##0")
    End Sub

    '## 박스당 갯수 구하는 함수
    Private Sub getEaOnBox()
        If autoCount = False Then
            Exit Sub
        End If
        If radSalesUnit2.Checked = True Then
            If cmbPrdCd.SelectedValue.ToString <> "0" Then
                txtTotalQty.Text = gFnEaOnBox(txtQty.Text, cmbPrdCd.SelectedValue.ToString)
            End If
        Else
            txtTotalQty.Text = txtQty.Text
        End If
    End Sub

    '## 출고 내역 저장
    Private Sub savePrdOut()

        Call dbConnection()
        Dim oleCmd As OleDb.OleDbCommand
        Dim oleTran As OleDb.OleDbTransaction
        Dim rtnMsg As String = ""
        Dim valSeq, valCompCd, valBoxQty, valUnitPrice, valTotalPrice, valPrdCd, valIssueDt, valInCnt, valRemarks, valCause, valFlag As String
        Dim curDt As DateTime = Now


        valPrdCd = cmbPrdCd.SelectedValue.ToString
        valCompCd = cmbCompany.SelectedValue.ToString
        valCause = cmbCause.SelectedValue.ToString
        valUnitPrice = txtUnitInPrice.Text
        valTotalPrice = txtTotalPrice.Text
        valIssueDt = dpIssueDt.Text
        valRemarks = gFnChkVal(txtRemarks.Text)
        valBoxQty = txtQty.Text
        valFlag = cmbFlag.SelectedValue.ToString
        valSeq = txtSeq.Text
        valInCnt = txtTotalQty.Text

        If valPrdCd = "0" Then
            dbConn.Close()
            MsgBox("제품을 먼저 선택해 주세요")
            cmbPrdCd.Focus()
            Exit Sub
        End If
        If txtTotalQty.Text = "" Then
            dbConn.Close()
            MsgBox("총수량을 바르게 입력해 주세요")
            txtTotalQty.Focus()
            Exit Sub
        End If

        If txtSeq.Text = "" Then
            sql = "INSERT INTO tblPrdInOut(prdCd, [input], unitPrice, totalPrice, remarks, compCd, amount, orgAmount, issueDt, boxQty, cause, flag, addDt) " + _
                "   VALUES('" + valPrdCd + "','" + valInCnt + "','" + valUnitPrice + "','" + valTotalPrice + "','" + valRemarks + "','" + valCompCd + "','" + valTotalPrice + "','" + valTotalPrice + "','" + valIssueDt + "','" + valBoxQty + "','" + valCause + "','" + valFlag + "','" + curDt + "')"
            rtnMsg = "입고 정보가 등록되었습니다"
        Else
            sql = "UPDATE tblPrdInOut SET prdCd='" + valPrdCd + "', [input]='" + valInCnt + "', compCd='" + valCompCd + "', amount='" + valTotalPrice + "', orgAmount='" + valTotalPrice + "', unitPrice='" + valUnitPrice + "', totalPrice='" + valUnitPrice + "', remarks='" + valRemarks + "', issueDt='" + valIssueDt + "', boxQty='" + valBoxQty + "', cause='" + valCause + "', flag='" + valFlag + "', updateDt='" + curDt + "' WHERE seq=" + valSeq
            rtnMsg = "입고 정보가 수정되었습니다"
        End If

        oleCmd = New OleDb.OleDbCommand(sql, dbConn)

        oleTran = dbConn.BeginTransaction
        oleCmd.Transaction = oleTran
        Try

            oleCmd.ExecuteNonQuery()
            oleTran.Commit()

            dbConn.Close()
            MsgBox(rtnMsg)
            Call gridRefresh("")
            Call txtReset()
        Catch err As OleDb.OleDbException
            oleTran.Rollback()
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
        '##  재고 조사 실시
        Call gLookupInventory(valPrdCd, "silence")
    End Sub

    '## 입출고 상세정보
    Private Sub prdInOutDetail(ByVal seq As String)
        Call txtReset()
        Call gSubMkCmbCompany("", cmbCompany) '## 회사 콤보 초기화

        Dim dsPrdInOutDetail As DataSet
        Dim daPrdInOutDetail As OleDb.OleDbDataAdapter
        sql = "SELECT seq, prdCd, [input], boxQty, unitPrice, totalPrice, compCd, cause, remarks, issueDt, flag  FROM tblPrdInOut WHERE seq=" & seq

        Call dbConnection()
        dsPrdInOutDetail = New DataSet
        daPrdInOutDetail = New OleDb.OleDbDataAdapter(sql, dbConn)
        daPrdInOutDetail.Fill(dsPrdInOutDetail, "PrdInOutInfo")

        dbConn.Close()

        txtSeq.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(0)

        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(1)) = False Then
            cmbPrdCd.SelectedValue = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(1)
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(2)) = False Then
            txtTotalQty.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(2)
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(3)) = False Then
            txtQty.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(3)
            If CInt(txtQty.Text) > 0 Then
                radSalesUnit2.Checked = True
            End If
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(4)) = False Then
            txtUnitInPrice.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(4)
            txtUnitInPrice.Text = Format(CInt(txtUnitInPrice.Text), "#,##0")
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(5)) = False Then
            txtTotalPrice.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(5)
            txtTotalPrice.Text = Format(CInt(txtTotalPrice.Text), "#,##0")
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(6)) = False Then
            cmbCompany.SelectedValue = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(6)
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(7)) = False Then
            cmbCause.SelectedValue = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(7)
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(8)) = False Then
            txtRemarks.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(8)
        End If
        If IsDBNull(dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(9)) = False Then
            dpIssueDt.Text = dsPrdInOutDetail.Tables("PrdInOutInfo").Rows(0).Item(9)
        End If
    End Sub

    Private Sub txtReset()
        cmbFlag.SelectedIndex = 0
        txtCompNm.Text = ""
        txtPrdNm.Text = ""
        txtSeq.Text = ""
        txtRemarks.Text = ""
        txtQty.Text = "1"
        txtTotalQty.Text = "1"
        radSalesUnit1.Checked = True
        txtTotalPrice.Text = "0"
        txtUnitInPrice.Text = "0"
        Call mkCmbProduct("", cmbPrdCd)
    End Sub
#End Region

#Region "라이브러리 함수"

    '## 제품 가져오기
    Private Sub mkCmbProduct(ByVal val As String, ByVal obj As Object)

        Dim addSql As String = ""
        prdCdChangeActionOn = False
        'Call dbConnection()
        If val <> "" Then
            prdCdChangeActionOn = True
            addSql = "WHERE prdNm LIKE '%" + val + "%' AND flag='Y' "
        End If
        sql = "SELECT prdNm as product, prdCd FROM tblProduct " + addSql + " ORDER BY prdNm"
        dsPrdInOut = New DataSet
        daPrdInOut = New OleDb.OleDbDataAdapter(sql, dbConn)
        daPrdInOut.Fill(dsPrdInOut, "PRD")

        Dim newDsRow As DataRow = dsPrdInOut.Tables("PRD").NewRow
        newDsRow("product") = "-제품-"
        newDsRow("prdCd") = "0"

        dsPrdInOut.Tables("PRD").Rows.Add(newDsRow)

        Try
            With obj
                .DataSource = dsPrdInOut.Tables("PRD")
                .DisplayMember = "product"
                .ValueMember = "prdCd"
                '.SelectedIndex = 0
            End With
            If addSql = "" Then
                obj.SelectedIndex = dsPrdInOut.Tables("PRD").Rows.Count - 1
            End If
        Catch err As NullReferenceException
            MsgBox(err.Message)
        End Try
        prdCdChangeActionOn = True
    End Sub

    '## 출고 사유
    Public Sub mkCmbOutCause(ByVal obj As Object)
        Dim sql As String
        'Call dbConnection()
        sql = "SELECT itemNm, itemCd FROM tblComCode WHERE flag='Y' AND groupCd='0010' ORDER BY itemSeq"
        dsPrdInOut = New DataSet
        daPrdInOut = New OleDb.OleDbDataAdapter(sql, dbConn)
        daPrdInOut.Fill(dsPrdInOut, "cmbCause")

        dbConn.Close()
        Dim newDsRow As DataRow = dsPrdInOut.Tables("cmbCause").NewRow
        newDsRow("itemNm") = "-출고사유-"
        newDsRow("itemCd") = "0"

        dsPrdInOut.Tables("cmbCause").Rows.Add(newDsRow)
        With obj
            .DataSource = dsPrdInOut.Tables("cmbCause")
            .DisplayMember = "itemNm"
            .ValueMember = "itemNm"
        End With
        obj.SelectedIndex = dsPrdInOut.Tables("cmbCause").Rows.Count - 1
    End Sub

    '## 입고 사유
    Public Sub mkCmbInCause(ByVal obj As Object)
        Dim sql As String
        Dim dsCommon As DataSet
        Dim daCommon As OleDb.OleDbDataAdapter
        'Call dbConnection()
        sql = "SELECT itemNm, itemCd FROM tblComCode WHERE flag='Y' AND groupCd='0009' ORDER BY itemSeq"
        dsCommon = New DataSet
        daCommon = New OleDb.OleDbDataAdapter(sql, dbConn)
        daCommon.Fill(dsCommon, "cmbCause")

        dbConn.Close()
        Dim newDsRow As DataRow = dsCommon.Tables("cmbCause").NewRow
        newDsRow("itemNm") = "-입고사유-"
        newDsRow("itemCd") = "0"

        dsCommon.Tables("cmbCause").Rows.Add(newDsRow)
        With obj
            .DataSource = dsCommon.Tables("cmbCause")
            .DisplayMember = "itemNm"
            .ValueMember = "itemNm"
        End With
        obj.SelectedIndex = dsCommon.Tables("cmbCause").Rows.Count - 1
    End Sub
#End Region

End Class